// pages/edit_party_info/edit_party_info.js
import {ajax} from "../../utils/httpRequest.js";
import {URI_GETBASEINFO, URI_GETBRANCHINFO, URI_EDITPARTYINFO} from "../../utils/api.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    branchdata: [],
    partyinfo: {
      branch_id: '',
      date: '',
      zz_time: '',
      job_id: '',
      partyaddress: '',
      organization: {
        organizationindex: '',
        organizationarray: []
      },
      post: '',
      active: true
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBranchInfo();
  },
  getBranchInfo() {
    ajax.get(URI_GETBRANCHINFO, {}, {}, res => {
      this.setData({
        branchdata: res.data.branchs
      });
      let branchs_name_array = [];
      this.data.branchdata.forEach((value) => {
        branchs_name_array.push(value.branchs_name);
      });
      this.setData({
        'partyinfo.organization.organizationarray': branchs_name_array
      });
      this.getBaseInfo();
    });
  },
  getBaseInfo() {
    ajax.get(URI_GETBASEINFO, {
      dangyaun_id: wx.getStorageSync('dangyuan_id')
    }, {}, res => {
      this.setData({
        'partyinfo.partyaddress': res.data.section2.org_address,
        'partyinfo.date': res.data.section2.join_time,
        'partyinfo.zz_time': res.data.section2.zz_time,
        'partyinfo.post': res.data.section2.title_name,
      });
      wx.setStorageSync('jobIdStr', res.data.section2.job_id);
      wx.setStorageSync('jobStr', res.data.section2.title_name);
      var branch_name = res.data.section2.branchs_name;
      this.data.partyinfo.organization.organizationarray.forEach((value, index) => {
        if (branch_name == value) {
          this.setData({
            'partyinfo.organization.organizationindex': index + '',
          })
        }
      });
      this.data.branchdata.forEach((value) => {
        //请求数据这是已选中的组织  ----  id
        if (branch_name == value.branchs_name) {
          this.setData({
            'partyinfo.branch_id': value.id
          });
          //请求数据这是已选中的党内 职务
          wx.setStorageSync('jobNameArray', value.titles);
        }
      });
    })
  },
  // clickBranch(){
  //   this.getBranchInfo2()
  // },
  // 党员信息录入部分开始------------------------------------------------------
  bindJoinDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      'partyinfo.date': e.detail.value
    })
  },
  zz_timeChange(e) {
    this.setData({
      'partyinfo.zz_time': e.detail.value
    })
  },
  bindOrganizationChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      'partyinfo.organization.organizationindex': e.detail.value,
    });
    let branch_name = this.data.partyinfo.organization.organizationarray[e.detail.value];
    this.data.branchdata.forEach((value) => {
      if (value.branchs_name == branch_name) {
        this.setData({
          'partyinfo.branch_id': value.id,
          'partyinfo.post': ''
        });
        wx.setStorageSync('jobNameArray', value.titles);
        wx.setStorageSync('jobStr', '');
        wx.setStorageSync('jobIdStr', '');
      }
    });
  },
  choose_job_item() {
    if (!this.data.partyinfo.branch_id) {
      wx.showModal({
        content: '请您先选择所属组织再选择党内职务',
        showCancel: false,
        confirmText: '确定',
        confirmColor: '#0d77e4',
        success: (res) => {
          if (res.confirm) {

          }
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/choose_job/choose_job',
      })
    }
  },
  bindPostChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'partyinfo.post.postindex': e.detail.value
    });
    let select_job_name = this.data.partyinfo.post.postarray[e.detail.value];
    // console.log(select_job_name);
    // console.log(this.data.branch_id);
    this.data.branchdata.forEach((value) => {
      if (value.id == this.data.partyinfo.branch_id) {
        value.titles.forEach((titles) => {
          if (titles.job_name == select_job_name) {
            this.setData({
              'partyinfo.job_id': titles.id
            })
          }
        })
      }
    });
    // console.log(this.data.partyinfo.branch_id);
    // console.log(this.data.partyinfo.job_id);
  },
  submitPartyInfo(e) {
    if (this.data.partyinfo.active) {
      let partyData = e.detail.value;
      if (!partyData.joinDate) {
        this.showMessage('请选择入党时间');
        return false;
      } else if (!partyData.organization) {
        this.showMessage('请选择所属组织');
        return false;
      } else if (!partyData.post) {
        this.showMessage('请选择党内职务');
        return false;
      } else if (!partyData.partymember) {
        this.showMessage('请输入您的组织关系所在地');
        return false;
      }

      console.log(this.data.partyinfo.branch_id);
      console.log(this.data.partyinfo.job_id);
      ajax.post(URI_EDITPARTYINFO, {
        id: wx.getStorageSync('dangyuan_id'),
        job_id: this.data.partyinfo.job_id,
        branch_id: this.data.partyinfo.branch_id,
        join_time: partyData.joinDate,
        org_address: partyData.partymember,
        zz_time: partyData.zz_time
      }, {}, res => {
        wx.showModal({
          title: '温馨提示',
          content: '党员信息已修改成功',
          showCancel: false,
          confirmText: '确定',
          confirmColor: '#0d77e4',
          success: (res) => {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      })
    } else {
      console.log('您还没有输入信息');
    }
  },

  showMessage(text) {
    wx.showToast({
      title: text,
      icon: 'none',
      duration: 2000
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      'partyinfo.job_id': wx.getStorageSync('jobIdStr'),
      'partyinfo.post': wx.getStorageSync('jobStr')
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})